using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;

namespace Ke.Bee.Transcription.Hubs;

/// <summary>
/// 创建一个 SignalR 聊天室
/// </summary>
/// <param name="logger"></param>
public class TranscriptionHub(ILogger<TranscriptionHub> logger) : Hub
{
    private readonly ILogger<TranscriptionHub> _logger = logger;

    public override async Task OnConnectedAsync()
    {
        _logger.LogInformation($"客户端连接: {Context.ConnectionId}");
        await Clients.Caller.SendAsync("Connected", new { message = "连接成功", connectionId = Context.ConnectionId });
        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception? exception)
    {
        _logger.LogInformation($"客户端断开连接: {Context.ConnectionId}");
        await base.OnDisconnectedAsync(exception);
    }

    /*
    /// <summary>
    /// 客户端可以调用此方法订阅特定任务的更新
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    public async Task SubscribeToTask(string taskId)
    {
        await Groups.AddToGroupAsync(Context.ConnectionId, taskId);
        _logger.LogInformation($"客户端 {Context.ConnectionId} 订阅任务 {taskId}");
        await Clients.Caller.SendAsync("Subscribed", new { taskId, message = "订阅成功" });
    }

    // 客户端可以调用此方法取消订阅
    public async Task UnsubscribeFromTask(string taskId)
    {
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, taskId);
        _logger.LogInformation($"客户端 {Context.ConnectionId} 取消订阅任务 {taskId}");
        await Clients.Caller.SendAsync("Unsubscribed", new { taskId, message = "取消订阅成功" });
    }

    // 客户端可以发送消息到服务器
    public async Task SendMessage(string message)
    {
        _logger.LogInformation($"收到客户端消息: {message}");
        await Clients.Caller.SendAsync("MessageReceived", new { message = $"服务器收到: {message}" });
    }
    */
}